// 2025/10/13
// 岛屿数量

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 判断岛屿数量
     * @param grid char字符型vector<vector<>> 
     * @return int整型
     */
    void sink(vector<vector<char>>& grid, int i, int j)
    {
        grid[i][j] = '0';
        int m = grid.size(), n = grid[0].size();
        auto isValid = [m, n](int i, int j){
            return (i >= 0 && i < m && j >= 0 && j < n);
        };

        static vector<pair<int, int>> dpos = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for(auto& pr : dpos)
        {
            int x = i + pr.first, y = j + pr.second;
            if(isValid(x, y) && grid[x][y] == '1')
            {
                sink(grid, x, y);
            }
        }
    }

    int solve(vector<vector<char> >& grid) {
        // write code here
        int ans = 0, m = grid.size(), n = grid[0].size();
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(grid[i][j] == '1')
                {
                    ans++;
                    sink(grid, i ,j);
                }
            }
        }
        return ans;
    }
};